package com.atguigu.spring.aspectJ.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 日志切面
 */

public class LoggingAspect {
	
	
	
	public void  beforeMethod(JoinPoint joinPoint) {
		//获取方法的参数
		Object [] args = joinPoint.getArgs();
		//方法的名字
		String methodName = joinPoint.getSignature().getName();
		
		System.out.println("LoggingAspect==> The method "+methodName+" begin with "+ Arrays.asList(args));
	}
	
	public void afterMethod(JoinPoint joinPoint) {
		//方法的名字
		String methodName = joinPoint.getSignature().getName();
		System.out.println("LoggingAspect==> The method " + methodName +" ends .");
		
	}
	
	public void afterReturningMethod(JoinPoint joinPoint,Object result ) {
		//方法的名字
		String methodName = joinPoint.getSignature().getName();
		System.out.println("LoggingAspect==> The method " + methodName + " end with :" + result );
	}
	
	
	public void afterThrowingMethod(JoinPoint joinPoint,ArithmeticException ex ) {
		//方法的名字
		String methodName = joinPoint.getSignature().getName();
		
		System.out.println("LoggingAspect==> Thew method " + methodName + " occurs Exception: " +ex );
	}
	
	public  Object  aroundMethod(ProceedingJoinPoint pjp) {
		
		//执行目标方法
		try {
			//前置
			Object result = pjp.proceed();
			//返回
			return result ; 
		} catch (Throwable e) {
			//异常通知
			e.printStackTrace();
		}finally {
			// 后置
		}
		
		return null;
	}
	
	
	
	
	
	
	
	
	
}
